

library(tidyverse)
library(rio)

# set replication folder as working directory
setwd("~replication")

load("data_genderedcost_background.rdata")

# only include completed answers - and answers given before deadline
# 2021-12-20 21:49:52 was the last response within the time frame
df_background <- df_background %>% 
  filter(SurveyStatus==2)

df_background <- df_background %>% 
  filter(SurveyEndTime<="2021-12-20 21:49:52")

##### Create Figure 1
### Victim - Harassment questionnarire - make seperate long data frame to facet on question
df_sexual_wide <- df_background %>% 
  select(c(id, sex, starts_with("kraenkelser")))

df_sexual <- df_sexual_wide %>% 
  pivot_longer(cols = !c(id,sex),
               names_to = c("question"),
               #names_sep = "_",
               values_to = "answer")

##  add labels to questionarie about sexual harassment
df_sexual <- df_sexual %>% 
  mutate(question = case_when(question=="kraenkelser_1_resp"~"Unwanted or degrading comments \nabout own body or sex",
                              question=="kraenkelser_2_resp"~"Being called names or mentioned \nin degrading ways",
                              question=="kraenkelser_3_resp"~"Being told that your voice is high-pitched \nor that you talked too much",
                              question=="kraenkelser_4_resp"~"To be sent or shown material with sexual \ncontent which was unwanted or offensive \nfor you",
                              question=="kraenkelser_5_resp"~"Unwanted physical touches"),
         answer = case_when(answer==1~"Yes",
                            answer==2~"No",
                            answer==3~"No")) %>% # put "don't want to answer" as
  mutate(question = factor(question, levels = c("Unwanted or degrading comments \nabout own body or sex",
                                                "Being called names or mentioned \nin degrading ways",
                                                "Being told that your voice is high-pitched \nor that you talked too much",
                                                "To be sent or shown material with sexual \ncontent which was unwanted or offensive \nfor you",
                                                "Unwanted physical touches")),
         answer = factor(answer, levels = c("No", "Yes", "Don't want to answer")))

# barplot over answer faceteret på question - og med fill = sex
# create shares of men, women and full sample
sample_shares_battery <- df_sexual %>%
  count(question, answer) %>%
  group_by(question) %>% 
  mutate(prop_answer = prop.table(n)) %>% 
  mutate(sex = factor("Full Sample"))

subset_shares_battery <- df_sexual %>% 
  count(sex, question, answer) %>% 
  group_by(question, sex) %>% 
  mutate(prop_answer = prop.table(n)) %>% 
  mutate(sex = factor(ifelse(sex=="Man","Men","Women")))

# add battery answers to aggregated df
shares_battery <- bind_rows(sample_shares_battery, subset_shares_battery)

# add overall victimhood to graph over battery answers
sample_shares <- df_background %>%
  count(victim) %>% 
  mutate(prop_victim = prop.table(n)) %>% 
  mutate(sex = factor("Full Sample"))

subset_shares <- df_background %>% 
  count(sex, victim) %>% 
  group_by(sex) %>% 
  mutate(prop_victim = prop.table(n)) %>% 
  mutate(sex = factor(ifelse(sex=="Man","Men","Women")))

shares <- bind_rows(sample_shares, subset_shares) %>%
  mutate(question = "Victims in total",
         answer = ifelse(victim=="Victim","Yes","No")) %>% 
  rename(prop_answer = prop_victim)


shares_total <- bind_rows(shares_battery, shares) %>% 
  mutate(question = factor(question, levels = c("Unwanted or degrading comments \nabout own body or sex",
                                                "Being called names or mentioned \nin degrading ways",
                                                "Being told that your voice is high-pitched \nor that you talked too much",
                                                "To be sent or shown material with sexual \ncontent which was unwanted or offensive \nfor you",
                                                "Unwanted physical touches",
                                                "Victims in total")))


### bar plot - faceted on type of harassment and total victim count
shares_total %>% 
  filter(answer=="Yes") %>%
  ggplot(data=., aes(x=answer, y=prop_answer, fill = sex)) +
  geom_col(color = "black", position = position_dodge2(width = 0.2)) +
  theme_bw() +
  ylab("Share") +
  xlab("") +
  scale_fill_grey("") +
  scale_color_grey("") +
  facet_wrap(~question, ncol = 3) +
  scale_y_continuous(labels = seq(0,0.5,0.1), breaks = seq(0,0.5,0.1)) +
  coord_cartesian(ylim = c(0,0.5)) +
  geom_text(aes(label = round(prop_answer,digits = 2)),
            position = position_dodge(width = 0.9), vjust = -0.5, size = 3) +
  theme(legend.position = "top",
        panel.background = element_rect(fill = "white"),
        strip.background = element_rect("white"),
        strip.text = element_text(hjust = 0),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())

ggsave("figure1.pdf", height = 5, width = 8)
ggsave("figure1.png", height = 5, width = 8, dpi = 600)
